package com.kaifamiao.demo.d0811;

import java.util.*;

/**
 * MyList 类， 数据类型
 *   容器，里面可以存放多个 元素. 变量， 数组
 *   弥补数组的缺点： 固定长度
 *   优点： 用户不用关注内部细节（不用管容器的大小）, 随机访问
 *
 *   银行系统， accounts  --> MyList  --> List
 *   重复造轮子
 */
public class MyList implements List {

    // 内部容器
    private Object[] objs;

    // 元素的个数
    private int count;

    // 负载因子
    private float capacity;

    public MyList(){
//        objs = new Object[10];
//        capacity = 0.8F;
        this(10);
    }

    public MyList(int size){
        if (size >= 0){
            objs = new Object[size];
        }else{
            objs = new Object[10];
        }
        capacity = 0.8F;
    }

    public MyList(Object[] objs){
        this.objs = objs;
        count = objs.length;
        capacity = 0.8F;
    }


    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count == 0;
    }

    @Override
    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }


    @Override
    public Iterator iterator() {
        return new MyListIterator();
    }

    /**
     * 自定义迭代器
     *   迭代 MyList 元素
     */
    private class MyListIterator implements Iterator {

        int index = -1;

        /**
         * 判断是否有下一个元素
         * @return
         */
        @Override
        public boolean hasNext() {
            return index < count - 1;
        }

        /**
         * 取下一个元素
         * @return
         */
        @Override
        public Object next() {
            if (hasNext()){
                return objs[++ index];
            }
            return null;
        }
    }


    @Override
    public Object[] toArray() {
        return Arrays.copyOf(objs, count);
    }


    /**
     * 添加元素
     * @param o  添加的元素
     * @return  true 添加成功
     */
    @Override
    public boolean add(Object o) {
        if (count >= objs.length * capacity) {
            // 扩容
            grow();
        }
        objs[count] = o;
        count ++;
        return true;
    }

    /**
     * 扩容
     */
    private void grow(){
        int oldSize = objs.length;
        // 1.5 倍
        int newSize = oldSize + (oldSize >> 1);
        // 将原来的元素复制到新的容器(数组)中
        objs = Arrays.copyOf(objs, newSize);
    }

    @Override
    public boolean remove(Object o) {
        int index = indexOf(o);
        if (index >= 0){
            System.arraycopy(objs, index + 1, objs, index, count - index - 1);
            objs[ -- count] = null;
            return true;
        }
        return false;
    }


    @Override
    public boolean addAll(Collection c) {
        // 可以不实现
        return false;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        // 可以不实现
        return false;
    }

    @Override
    public void clear() {
        Arrays.fill(objs, null);
        count = 0;
    }

    @Override
    public Object get(int index) {
        checkIndex(index);

        return objs[index];
    }

    private void checkIndex(int index) {
        if (index < 0 || index > count) {
            throw new IndexOutOfBoundsException("下标不合法：" + index);
        }
    }

    @Override
    public Object set(int index, Object element) {
        if (index >= 0 && index < count){
            Object old = objs[index];
            objs[index] = element;
            return old;
        }
        return null;
    }

    @Override
    public void add(int index, Object element) {
        if (count + 1 > objs.length * capacity){
            grow();
        }
        System.arraycopy(objs, index, objs, index + 1, count - index - 1);
        objs[index] = element;
        count ++;
    }

    @Override
    public Object remove(int index) {
        if (index >=0 && index < count){
            Object old = objs[index];
            System.arraycopy(objs, index + 1, objs, index, count - index - 1);
            objs[ -- count] = null;
            return old;
        } else {
            throw new IndexOutOfBoundsException("下标不合法");
        }
    }

    @Override
    public int indexOf(Object o) {
        for (int i = 0; i < count; i ++){
            if (objs[i] == null && o == null){
                return i;
            } else if (objs[i] != null && objs[i].equals(o)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        for (int i = count - 1; i >= 0; i --){
            if (objs[i] == null && o == null){
                return i;
            } else if (objs[i] != null && objs[i].equals(o)){
                return i;
            }
        }
        return -1;
    }



    @Override
    public ListIterator listIterator() {
        // 可以不实现
        return null;
    }

    @Override
    public ListIterator listIterator(int index) {
        // 可以不实现
        return null;
    }

    @Override
    public MyList subList(int fromIndex, int toIndex) {
        if (fromIndex >= 0 && fromIndex < count && fromIndex <= toIndex && toIndex <= count){
            Object[] objects = new Object[toIndex - fromIndex];
            System.arraycopy(objs, fromIndex, objects, 0, objects.length);
            return new MyList(objects);
        } else{
            throw new IndexOutOfBoundsException("下标不合法：fromIndex" + fromIndex + ", toIndex" + toIndex);
        }
    }

    @Override
    public boolean retainAll(Collection c) {
        // 可以不实现
        return false;
    }

    @Override
    public boolean removeAll(Collection c) {
        // 可以不实现
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        // 可以不实现
        return false;
    }

    @Override
    public Object[] toArray(Object[] a) {
        // 不实现
        return new Object[0];
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("MyList[");
        for (int i = 0; i < count; i ++){
            sb.append(objs[i] + ", ");
        }

        return sb.substring(0, sb.length() - 2) + "]";
    }
}
